﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.Data
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid

Public Class TC_NganHang1
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Dim DoanhThu, PhanTramThue, TienThue, TongCong As Decimal
    Dim MaKhachHang, Ten, DiaChi, MaSoThue As String
    Dim TaiKhoanNo, TaiKhoanCo, TaiKhoanThue As String
    Private flag As Boolean = False 'tránh loop xtragrid
    Private Sub setReadonlyControl(ByVal b As Boolean)
        setReadonly(Me.layoutControl, b)
        If b Then
            Me.IGridViewTienChiTiet.OptionsBehavior.Editable = False
            Me.IGridViewTienChiTiet.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else
            Me.IGridViewTienChiTiet.OptionsBehavior.Editable = True
            Me.IGridViewTienChiTiet.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.TCTienTableAdapter.Fill(Me.PCV_FISHDataSet.TCTien)
        Me.TCThueTableAdapter.Fill(Me.PCV_FISHDataSet.TCThue)
        Me.TCTienChiTietTableAdapter.Fill(Me.PCV_FISHDataSet.TCTienChiTiet)
        Me.DmkhachhangTableAdapter.FillByMaTenDiaChi(Me.PCV_FISHDataSet.dmkhachhang)
        Me.DmnghiepvuTableAdapter.Fill(Me.PCV_FISHDataSet.dmnghiepvu)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.DmnguonvonTableAdapter.Fill(Me.PCV_FISHDataSet.dmnguonvon)
        Me.DmkheuocvayTableAdapter.Fill(Me.PCV_FISHDataSet.dmkheuocvay)
        Me.DmkhoanmucphiTableAdapter.Fill(Me.PCV_FISHDataSet.dmkhoanmucphi)
        Me.DmnganhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmnganhang)
        Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, Today)
        Me.DmphongbanTableAdapter.Fill(Me.PCV_FISHDataSet.dmphongban)
        Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        Me.DmtaikhoannganhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmtaikhoannganhang)
        setReadonlyControl(True)
        Me.TC_TienIGridControl.ForceInitialize()
        Me.TCTienBindingSource.MoveLast()
        ' Me.ManghiepvuISearchLookupEdit.setNotShowPopUpEnter()
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()

    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonlyControl(False)
        Me.ManghiepvuISearchLookupEdit.Focus()
        Me.ManghiepvuISearchLookupEdit.ShowPopup()
        'Mã //set ngầm
        Dim row As DataRowView = CType(TCTienBindingSource.AddNew(), DataRowView)
        Me.Ma = "TCNH" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        'mặc định
        Me.NgaychungtuIDateEdit.EditValue = Today
        'Me.NgayThamChieuDateEdit.EditValue = Today
        Me.TygiaNganHangITextEdit.EditValue = 1
        Me.LoaitienISearchLookupEdit.EditValue = "VND"
        Me.ThanhTienvndITextEdit.EditValue = "0"
        Me.NguyenteITextEdit.EditValue = "0"
        Me.TinhtrangISearchLookupEdit.EditValue = "NL"
        Me.State = 1
    End Sub

    Private Sub MakhachhangSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MaKhachhangISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MaKhachhangISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        ' Me.TenkhachhangTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Dim row As DataRowView = CType(TCTienBindingSource.Current, DataRowView)
        row("tendonvi") = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.DiachiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
    End Sub
    Private Sub setCongNoPhaiThu() 'xử lý công nợ phải thu 
        Dim sochungtu As String = SochungtuITextEdit.EditValue
        Dim ma As String = Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma").ToString
        Dim ngaychungtu As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
        Dim madoituong As String = Me.MakhachhangISearchLookupEdit.EditValue
        Dim tendoituong As String = Me.MakhachhangISearchLookupEdit.Text
        Dim diachidoituong As String = Me.DiachiITextEdit.EditValue
        Dim sohoadon As String = Me.SochungtuITextEdit.EditValue
        Dim ngayhoadon As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
        Dim hanthanhtoan As Date = CDate(NgaychungtuIDateEdit.EditValue)
        Dim loaitien As String = Me.LoaitienISearchLookupEdit.EditValue
        Dim tygia As Decimal = CDec(Me.TygianganhangITextEdit.EditValue)
        Dim nguyente As Decimal = CDec(Me.NguyenteITextEdit.EditValue)
        Dim thanhtienvnd As Decimal = CDec(Me.ThanhtienvndITextEdit.EditValue)
        Dim taikhoanco_thongtin As String = Me.PCV_FISHDataSet.DMNghiepVu.Rows(Me.DmnghiepvuBindingSource.Find("ma", Me.ManghiepvuISearchLookupEdit.EditValue)).Item("taikhoanco").ToString
        Dim kyketoan As String = CDate(NgaychungtuIDateEdit.EditValue).Year & Format(CDate(NgaychungtuIDateEdit.EditValue).Month, "00")
        If Me.TCCongNoPhaiThuTableAdapter.getTCCongNoPhaiThu_isTonTaiUid(getGuid(ma, 4)) = 1 Then
            Me.TCCongNoPhaiThuTableAdapter.Update(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, 0, 0, _
                                                  nguyente, thanhtienvnd, 0, 0, 0, 0, taikhoanco_thongtin, "", kyketoan, "", ma, getGuid(ma, 4), ma, getGuid(ma, 4), 0)
        Else
            Me.TCCongNoPhaiThuTableAdapter.Insert(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, 0, 0, _
                                                 nguyente, thanhtienvnd, 0, 0, 0, 0, taikhoanco_thongtin, "", kyketoan, "", ma, getGuid(ma, 4))
        End If
        'chi tiết
        For index = 0 To Me.IGridViewTienChiTiet.RowCount - 1
            Dim uid As System.Guid = Me.IGridViewTienChiTiet.GetRowCellValue(index, "uid")
            Dim madoituongchitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "madonvi")
            Dim tendoituongchitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "tendonvi")
            Dim diachidoituongchitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "diachidonvi")
            Dim taikhoanco_chitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "taikhoanco")
            Dim nguyentechitiet As Decimal = Me.IGridViewTienChiTiet.GetRowCellValue(index, "nguyente")
            Dim thanhtienvndchitiet As Decimal = Me.IGridViewTienChiTiet.GetRowCellValue(index, "tienvnd")
            If Me.TCCongNoPhaiThuTableAdapter.getTCCongNoPhaiThu_isTonTaiUid(uid) = 1 Then
                Me.TCCongNoPhaiThuTableAdapter.Update(sochungtu, ngaychungtu, madoituongchitiet, tendoituongchitiet, diachidoituongchitiet, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, 0, 0, _
                                                      nguyentechitiet, thanhtienvndchitiet, 0, 0, 0, 0, taikhoanco_chitiet, "", kyketoan, "", ma, uid, ma, uid, 0)
            Else
                Me.TCCongNoPhaiThuTableAdapter.Insert(sochungtu, ngaychungtu, madoituongchitiet, tendoituongchitiet, diachidoituongchitiet, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, 0, 0, _
                                                     nguyentechitiet, thanhtienvndchitiet, 0, 0, 0, 0, taikhoanco_chitiet, "", kyketoan, "", ma, uid)
            End If
        Next
    End Sub
    Private Sub setCongNoPhaiTra() 'xử lý công nợ phải thu 
        Dim sochungtu As String = SochungtuITextEdit.EditValue
        Dim ma As String = Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma").ToString
        Dim ngaychungtu As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
        Dim madoituong As String = Me.MaKhachhangISearchLookupEdit.EditValue
        Dim tendoituong As String = Me.MaKhachhangISearchLookupEdit.Text
        Dim diachidoituong As String = Me.DiachiITextEdit.EditValue
        Dim sohoadon As String = Me.SochungtuITextEdit.EditValue
        Dim ngayhoadon As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
        Dim hanthanhtoan As Date = CDate(NgaychungtuIDateEdit.EditValue)
        Dim loaitien As String = Me.LoaitienISearchLookupEdit.EditValue
        Dim tygia As Decimal = CDec(Me.TygiaNganHangITextEdit.EditValue)
        Dim nguyente As Decimal = CDec(Me.NguyenteITextEdit.EditValue)
        Dim thanhtienvnd As Decimal = CDec(Me.ThanhTienvndITextEdit.EditValue)
        Dim taikhoanno_thongtin As String = Me.PCV_FISHDataSet.DMNghiepVu.Rows(Me.DmnghiepvuBindingSource.Find("ma", Me.ManghiepvuISearchLookupEdit.EditValue)).Item("taikhoanno").ToString
        Dim kyketoan As String = CDate(NgaychungtuIDateEdit.EditValue).Year & Format(CDate(NgaychungtuIDateEdit.EditValue).Month, "00")
        If Me.TCCongNoPhaiTraTableAdapter.getTCCongNoPhaiTra_isTonTaiUid(getGuid(ma, 4)) = 1 Then
            Me.TCCongNoPhaiTraTableAdapter.Update(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, nguyente, thanhtienvnd, _
                                                 0, 0, 0, 0, 0, 0, taikhoanno_thongtin, "", kyketoan, "", ma, getGuid(ma, 4), ma, getGuid(ma, 4), 0)
        Else
            Me.TCCongNoPhaiTraTableAdapter.Insert(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, nguyente, thanhtienvnd, _
                                                 0, 0, 0, 0, 0, 0, taikhoanno_thongtin, "", kyketoan, "", ma, getGuid(ma, 4))
        End If
        'chi tiết
        For index = 0 To Me.IGridViewTienChiTiet.RowCount - 1
            Dim uid As System.Guid = Me.IGridViewTienChiTiet.GetRowCellValue(index, "uid")
            Dim madoituongchitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "madonvi")
            Dim tendoituongchitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "tendonvi")
            Dim diachidoituongchitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "diachidonvi")
            Dim taikhoanno_chitiet As String = Me.IGridViewTienChiTiet.GetRowCellValue(index, "taikhoanno")
            Dim nguyentechitiet As Decimal = Me.IGridViewTienChiTiet.GetRowCellValue(index, "nguyente")
            Dim thanhtienvndchitiet As Decimal = Me.IGridViewTienChiTiet.GetRowCellValue(index, "tienvnd")
            If Me.TCCongNoPhaiTraTableAdapter.getTCCongNoPhaiTra_isTonTaiUid(uid) = 1 Then
                Me.TCCongNoPhaiTraTableAdapter.Update(sochungtu, ngaychungtu, madoituongchitiet, tendoituongchitiet, diachidoituongchitiet, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, nguyentechitiet, thanhtienvndchitiet, _
                                                      0, 0, 0, 0, 0, 0, taikhoanno_chitiet, "", kyketoan, "", ma, uid, ma, uid, 0)
            Else
                Me.TCCongNoPhaiTraTableAdapter.Insert(sochungtu, ngaychungtu, madoituongchitiet, tendoituongchitiet, diachidoituongchitiet, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, nguyentechitiet, thanhtienvndchitiet, _
                                                     0, 0, 0, 0, 0, 0, taikhoanno_chitiet, "", kyketoan, "", ma, uid)
            End If
        Next
    End Sub
    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewThue.setUpdateData()
            Me.IGridViewTienChiTiet.setUpdateData()
            Me.TCTienBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.FKTCTienChiTietTCTienBindingSource.EndEdit()
            Me.FKTCThueTCThue1BindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonlyControl(True) 'set lại readonly
                        setCongNoPhaiThu() 'set công nợ phải thu
                        setCongNoPhaiTra() 'set công nợ phải trả
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonlyControl(True) 'set lại readonly
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    Private Sub NhanviensaleSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MabophanISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MabophanISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        'Me.DmphongbanBindingSource.Filter = "ma = '" & editor.Properties.View.GetRowCellValue(rowhandle, "bophan") & "'"
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonlyControl(False)
        Me.State = 2
    End Sub

    Private Sub IGridViewTenChiTiet_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewTienChiTiet.CellValueChanged
        Dim view1 As GridView = CType(sender, GridView)
        Dim nguyente, tienvnd As Decimal
        If (e.Column.FieldName = "tienvnd" OrElse e.Column.FieldName = "nguyente") AndAlso (Not flag) Then
            Try
                nguyente = CDec(view1.GetRowCellValue(e.RowHandle, view1.Columns("nguyente")))
                tienvnd = nguyente * CDec(Me.TygiaNganHangITextEdit.EditValue)
            Catch ex As Exception
                Exit Sub
            End Try
            flag = True
            view1.SetRowCellValue(e.RowHandle, view1.Columns("tienvnd"), tienvnd)
            flag = False
        End If
        Me.NguyenteITextEdit.EditValue = CDec(Me.IGridViewTienChiTiet.Columns("nguyente").SummaryItem.SummaryValue)
        Me.ThanhTienvndITextEdit.EditValue = CDec(Me.IGridViewTienChiTiet.Columns("tienvnd").SummaryItem.SummaryValue)
        Me.IGridViewTienChiTiet.UpdateCurrentRow()
        Me.IGridViewTienChiTiet.UpdateSummary()
    End Sub

    Private Sub IGridViewTienChiTiet_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridViewTienChiTiet.FocusedRowChanged
        'Me.DoanhThu = 0
        'Me.TienThue = 0
        Dim currentView As GridView = CType(sender, GridView)
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            Try
                If Me.IGridViewTienChiTiet.GetGroupRowValue(Me.IGridViewTienChiTiet.FocusedRowHandle) = "133" Then
                    Me.TienThue += CDec(val)
                Else
                    Me.DoanhThu += CDec(val)
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub
    Private Sub IGridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewTienChiTiet.InitNewRow
        'If State = 1 Then
        '    Me.IGridViewTienChiTiet.SetFocusedRowCellValue("ma", Ma)
        'End If
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuIDateEdit.EditValue))
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("phantramthue", 0) 'phân biệt chữ hoa thường
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("nguyente", 0)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tienvnd", 0)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tienthue", 0)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tongcong", 0)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("taikhoanno", TaiKhoanNo)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("taikhoanco", TaiKhoanCo)
        ' Me.IGridViewTienChiTiet.SetFocusedRowCellValue("taikhoanthueGTGT", TaiKhoanThue)
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        Me.IGridViewTienChiTiet.MoveLast()
        Me.IGridViewTienChiTiet.Focus()
        Me.IGridViewTienChiTiet.AddNewRow()
        Me.IGridViewTienChiTiet.FocusedColumn = Me.IGridViewTienChiTiet.VisibleColumns(0)
        Me.IGridViewTienChiTiet.ShowEditor()
    End Sub

    Private Sub LoaitienSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles LoaitienISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.LoaitienISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TygiaNganHangITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "tygia")
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.TCTienBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonPrevious.ItemClick
        Me.TCTienBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNext.ItemClick
        Me.TCTienBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonLast.ItemClick
        Me.TCTienBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        If TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKTCTienChiTietTCTienBindingSource.MoveFirst()
        End If
        If TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKTCThueTCThue1BindingSource.MoveFirst()
        End If
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        If TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKTCTienChiTietTCTienBindingSource.MovePrevious()
        End If
        If TabbedControlGroup2.SelectedTabPageIndex = 1 Then
            Me.FKTCThueTCThue1BindingSource.MovePrevious()
        End If
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKTCTienChiTietTCTienBindingSource.MoveNext()
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 1 Then
            Me.FKTCThueTCThue1BindingSource.MoveNext()
        End If
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        If TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKTCTienChiTietTCTienBindingSource.MoveLast()
        End If
        If TabbedControlGroup2.SelectedTabPageIndex = 1 Then
            Me.FKTCThueTCThue1BindingSource.MoveLast()
        End If
    End Sub
    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.SochungtuITextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.TCTienBindingSource.RemoveCurrent()
            Me.TCThueTableAdapter.Update(Me.PCV_FISHDataSet.TCThue)
            Me.setReadonlyControl(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.TCTienBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewTienChiTiet.GetRowCellValue(Me.IGridViewTienChiTiet.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewTienChiTiet.DeleteRow(Me.IGridViewTienChiTiet.FocusedRowHandle)
            End If
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 1 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewThue.GetRowCellValue(Me.IGridViewTienChiTiet.FocusedRowHandle, "sohoadon") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewThue.DeleteRow(Me.IGridViewThue.FocusedRowHandle)
            End If
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Me.FKTCTienChiTietTCTienBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần 'hàng hóa' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        'load dữ liệu lại nhưng giữ nguyên phiếu
        Dim position As Integer = Me.TCTienBindingSource.Position
        loadDuLieu()
        Me.TCTienBindingSource.Position = position
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKhachHang_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKhachHang.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("madonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tendonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("diachidonvi", editor.Properties.View.GetRowCellValue(rowHandle, "diachi"))
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKheUocVay_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKheUocVay.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("kheuocvay", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("hopdongtindung", editor.Properties.View.GetRowCellValue(rowHandle, "sohopdongtindung"))
    End Sub

    Private Sub BarButtonItemChuyenDuLieuThue_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemChuyenDuLieuThue.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.IGridViewTienChiTiet.Columns("taikhoanno").GroupIndex = 0
            Me.IGridViewTienChiTiet.Columns("sohoadon").GroupIndex = 1
            For index = 0 To Me.IGridViewTienChiTiet.RowCount - 1
                Me.IGridViewTienChiTiet.MoveNext()
            Next
            Me.IGridViewTienChiTiet.ClearGrouping()
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(1) 'chọn tab thue
            Me.IGridViewThue.AddNewRow()
        End If
    End Sub

    Private Sub IGridViewThue_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewThue.InitNewRow
        'set mặc định
        Me.IGridViewThue.SetFocusedRowCellValue("ma", Ma)
        Me.IGridViewThue.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuIDateEdit.EditValue))
        '==================================================================
        Me.TongCong = CDec(Me.IGridViewTienChiTiet.Columns("tienvnd").SummaryItem.SummaryValue)
        Try
            Me.PhanTramThue = Me.TienThue / Me.DoanhThu * 100
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Me.IGridViewThue.SetFocusedRowCellValue("madonvi", Me.MaKhachHang)
        Me.IGridViewThue.SetFocusedRowCellValue("tendonvi", Me.Ten)
        Me.IGridViewThue.SetFocusedRowCellValue("diachi", Me.DiaChi)
        Me.IGridViewThue.SetFocusedRowCellValue("masothue", Me.MaSoThue)
        Me.IGridViewThue.SetFocusedRowCellValue("doanhthu", Me.DoanhThu)
        Me.IGridViewThue.SetFocusedRowCellValue("phantramthue", Me.PhanTramThue) 'phân biệt chữ hoa thường
        Me.IGridViewThue.SetFocusedRowCellValue("tienthue", Me.TienThue)
        Me.IGridViewThue.SetFocusedRowCellValue("tongcong", Me.TongCong)
    End Sub

    'Private Sub BarButtonItemHelp_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHelp.ItemClick
    '    'Dim position As Integer = Me.DmkhachhangBindingSource.Find("ma", Me.MakhachhangSearchLookUpEdit.EditValue)
    '    'MessageBox.Show(position)

    '    'Dim sodienthoai As String = Me.PCV_FISHDataSet.dmkhachhang.Rows(position).Item("dienthoai").ToString()
    '    'Dim masothue As String = Me.PCV_FISHDataSet.dmkhachhang.Rows(position).Item("masothue").ToString()
    '    'MessageBox.Show(sodienthoai)
    '    'MessageBox.Show(masothue)
    'End Sub

    Private Sub TinhtrangSearchLookUpEdit_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TinhtrangISearchLookupEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(0)
            Me.TCTienBindingSource.EndEdit()
            Me.IGridViewTienChiTiet.MoveLast()
            Me.IGridViewTienChiTiet.Focus()
            Me.IGridViewTienChiTiet.AddNewRow()
            Me.IGridViewTienChiTiet.FocusedColumn = Me.IGridViewTienChiTiet.VisibleColumns(0)
            Me.IGridViewTienChiTiet.ShowEditor()
        End If

    End Sub

    Private Sub ManghiepvuSearchLookUpEdit_EditValueChanging(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles ManghiepvuISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.ManghiepvuISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TaiKhoanNo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanno")
        Me.TaiKhoanCo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanco")
        ' Me.TaiKhoanThue = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanvat")
    End Sub
End Class
